package com.shiguiwu.springboot3.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageParam;
import com.shiguiwu.springboot3.dto.request.CreateSessionReq;
import com.shiguiwu.springboot3.dto.request.SessionPageReq;
import com.shiguiwu.springboot3.dto.response.MySessionResponse;
import com.shiguiwu.springboot3.entity.ChatSession;

import com.shiguiwu.springboot3.mapper.ChatSessionMapper;
import com.shiguiwu.springboot3.util.MemberContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 会话服务
 * @author: stone
 * @date: Created by 2025/10/31 22:32
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.service
 */
@Service
public class ChatSessionService {

    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private ChatSessionMapper chatSessionMapper;


    /**
     * 创建会话
     *
     * @param req
     * @return
     */
    public Long createSession(CreateSessionReq req) {
        ChatSession chatSession = new ChatSession();
        chatSession.setCreatedAt(LocalDateTime.now());
        chatSession.setUpdatedAt(LocalDateTime.now());

        chatSession.setUserId(MemberContextUtils.getCurrentUserId());
        chatSession.setToUserId(req.getToUserId());
        chatSession.setSessionName("与" + req.getToUserName() + "对话");
        chatSessionMapper.insert(chatSession);
        return chatSession.getId();
    }

    public PageInfo<MySessionResponse> mySessions(SessionPageReq req, PageParam pageParam) {
        // 关键一步：在查询开始前设置分页参数
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        // 执行查询，这个 SQL 会被自动加上分页语句
        Example example = new Example(ChatSession.class);
        example.createCriteria().andEqualTo("user_id", MemberContextUtils.getCurrentUserId())
                .orEqualTo("to_user_id", MemberContextUtils.getCurrentUserId())
                .andLike("session_name", req.getSessionName());

        example.setOrderByClause("create_at desc");
        // 使用 PageInfo 包装结果，它包含了丰富的分页信息
        List<ChatSession> chatSessions = chatSessionMapper.selectByExample(example);
        PageInfo<ChatSession> pageInfo = new PageInfo<>(chatSessions);

        List<MySessionResponse> mySessionResponses = new ArrayList<>();
        pageInfo.getList().forEach(session -> {
            MySessionResponse mySessionResponse = new MySessionResponse();
            mySessionResponse.setSessionId(session.getId());
            mySessionResponse.setSessionName(session.getSessionName());
            mySessionResponse.setUnReadCount(chatMessageService.queryUnreadCount(session.getId()));
            mySessionResponses.add(mySessionResponse);
        });

        PageInfo<MySessionResponse> result = new PageInfo<>();
        BeanUtil.copyProperties(pageInfo, result);
        result.setList(mySessionResponses);
        return result;
    }


}
